package com.fintek.mx.mxMarket.base

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.blankj.utilcode.util.LogUtils
import com.fintek.mx.mxMarket.ui.start.GuideDataListener

class YicFragmentAdapter : FragmentPagerAdapter {

    private val mFragmentList: MutableList<Fragment>
    private val tagList = mutableListOf<Long>()

    constructor(fragmentManager: FragmentManager) : super(
        fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
    ) {
        mFragmentList = ArrayList()
    }

    constructor(fragmentManager: FragmentManager, mFragmentList: MutableList<Fragment>) : super(
        fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
    ) {
        this.mFragmentList = mFragmentList
    }

    fun removeAfter(index: Int) {
        mFragmentList.removeAll(mFragmentList.subList(index, mFragmentList.size))
        tagList.clear()
        notifyDataSetChanged()
    }

//    override fun getItemPosition(`object`: Any): Int {
//        return if (tagList.contains(`object`.hashCode().toLong())) {
//            POSITION_NONE // POSITION_UNCHANGED
//        } else {
//            POSITION_NONE
//        }
//    }

    fun addFragment(fragmentList: List<Fragment>?) {
        if (fragmentList == null) {
            return
        }
        mFragmentList.addAll(fragmentList)
        tagList.clear()
        notifyDataSetChanged()
    }

    override fun getItem(position: Int): Fragment {
        return mFragmentList[position]
    }

    override fun getItemId(position: Int): Long {
        val toLong = getItem(position).hashCode().toLong()
        tagList.add(toLong)
        return toLong
    }

    override fun getCount(): Int {
        return mFragmentList.size
    }

    override fun getPageTitle(position: Int): CharSequence? {
        val item = getItem(position)
        return if (item is GuideDataListener) {
            (item as GuideDataListener).getTitleCharSequence()
        } else item.javaClass.name
    }
}